

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>argoverse.data_loading.frame_label_accumulator &mdash; argoverse  documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> argoverse
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">argoverse</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>argoverse.data_loading.frame_label_accumulator</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for argoverse.data_loading.frame_label_accumulator</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>

<span class="c1"># &lt;Copyright 2019, Argo AI, LLC. Released under the MIT license.&gt;</span>

<span class="kn">import</span> <span class="nn">glob</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pickle</span> <span class="k">as</span> <span class="nn">pkl</span>
<span class="kn">import</span> <span class="nn">tempfile</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="k">import</span> <span class="n">Path</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Tuple</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">argoverse.data_loading.frame_record</span> <span class="k">import</span> <span class="n">FrameRecord</span>
<span class="kn">from</span> <span class="nn">argoverse.data_loading.object_label_record</span> <span class="k">import</span> <span class="n">ObjectLabelRecord</span>
<span class="kn">from</span> <span class="nn">argoverse.data_loading.pose_loader</span> <span class="k">import</span> <span class="n">get_city_SE3_egovehicle_at_sensor_t</span>
<span class="kn">from</span> <span class="nn">argoverse.data_loading.synchronization_database</span> <span class="k">import</span> <span class="n">SynchronizationDB</span>
<span class="kn">from</span> <span class="nn">argoverse.data_loading.trajectory_loader</span> <span class="k">import</span> <span class="n">TrajectoryLabel</span><span class="p">,</span> <span class="n">load_json_track_labels</span>
<span class="kn">from</span> <span class="nn">argoverse.utils.json_utils</span> <span class="k">import</span> <span class="n">read_json_file</span>
<span class="kn">from</span> <span class="nn">argoverse.utils.pkl_utils</span> <span class="k">import</span> <span class="n">load_pkl_dictionary</span><span class="p">,</span> <span class="n">save_pkl_dictionary</span>
<span class="kn">from</span> <span class="nn">argoverse.utils.se3</span> <span class="k">import</span> <span class="n">SE3</span>
<span class="kn">from</span> <span class="nn">argoverse.utils.transform</span> <span class="k">import</span> <span class="n">quat2rotmat</span>

<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="vm">__name__</span><span class="p">)</span>


<div class="viewcode-block" id="PerFrameLabelAccumulator"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.frame_label_accumulator.PerFrameLabelAccumulator">[docs]</a><span class="k">class</span> <span class="nc">PerFrameLabelAccumulator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot; We will cache the accumulated track label trajectories per city, per log, and per frame.</span>
<span class="sd">    In order to plot each frame sequentially, one at a time, we need to aggregate beforehand</span>
<span class="sd">    the tracks and cuboids for each frame.</span>

<span class="sd">    Attributes:</span>
<span class="sd">        bboxes_3d (bool): to use 3d bounding boxes (True) or 2d bounding boxes (False).</span>
<span class="sd">        dataset_dir (str): Dataset directory.</span>
<span class="sd">        labels_dir (str): Labels directory.</span>
<span class="sd">        log_egopose_dict (dict): Egopose per log id and timestamp.</span>
<span class="sd">        log_timestamp_dict (dict): List of frame records per log id and timestamp.</span>
<span class="sd">        per_city_traj_dict (dict): Per city trajectory dictionary.</span>
<span class="sd">        sdb (SynchronizationDB): Synchronization DB.</span>
<span class="sd">    &quot;&quot;&quot;</span>

<div class="viewcode-block" id="PerFrameLabelAccumulator.__init__"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.frame_label_accumulator.PerFrameLabelAccumulator.__init__">[docs]</a>    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">dataset_dir</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">labels_dir</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">experiment_prefix</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">bboxes_3d</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span> <span class="n">save</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot; Initialize PerFrameLabelAccumulator object for use with tracking benchmark data.</span>

<span class="sd">        Args:</span>
<span class="sd">            dataset_dir (str): Dataset directory.</span>
<span class="sd">            labels_dir (str): Labels directory.</span>
<span class="sd">            experiment_prefix (str): Prefix for experimint to use.</span>
<span class="sd">            bboxes_3d (bool, optional): to use 3d bounding boxes (True) or 2d bounding boxes (False).</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bboxes_3d</span> <span class="o">=</span> <span class="n">bboxes_3d</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">dataset_dir</span> <span class="o">=</span> <span class="n">dataset_dir</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">labels_dir</span> <span class="o">=</span> <span class="n">labels_dir</span>
        <span class="n">tmp_dir</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">gettempdir</span><span class="p">()</span>
        <span class="n">per_city_traj_dict_fpath</span> <span class="o">=</span> <span class="n">f</span><span class="s2">&quot;</span><span class="si">{tmp_dir}</span><span class="s2">/per_city_traj_dict_</span><span class="si">{experiment_prefix}</span><span class="s2">.pkl&quot;</span>
        <span class="n">log_egopose_dict_fpath</span> <span class="o">=</span> <span class="n">f</span><span class="s2">&quot;</span><span class="si">{tmp_dir}</span><span class="s2">/log_egopose_dict_</span><span class="si">{experiment_prefix}</span><span class="s2">.pkl&quot;</span>
        <span class="n">log_timestamp_dict_fpath</span> <span class="o">=</span> <span class="n">f</span><span class="s2">&quot;</span><span class="si">{tmp_dir}</span><span class="s2">/log_timestamp_dict_</span><span class="si">{experiment_prefix}</span><span class="s2">.pkl&quot;</span>

        <span class="n">coordinate_system</span> <span class="o">=</span> <span class="s2">&quot;map_world_fr&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">per_city_traj_dict</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="nb">str</span><span class="p">]]]</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;MIA&quot;</span><span class="p">:</span> <span class="p">[],</span>
            <span class="s2">&quot;PIT&quot;</span><span class="p">:</span> <span class="p">[],</span>
        <span class="p">}</span>  <span class="c1"># all the trajectories for these 2 cities</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">log_egopose_dict</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">log_timestamp_dict</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="n">FrameRecord</span><span class="p">]]]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">sdb</span> <span class="o">=</span> <span class="n">SynchronizationDB</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dataset_dir</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">save</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">accumulate_per_log_data</span><span class="p">()</span>
            <span class="n">save_pkl_dictionary</span><span class="p">(</span><span class="n">per_city_traj_dict_fpath</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">per_city_traj_dict</span><span class="p">)</span>
            <span class="n">save_pkl_dictionary</span><span class="p">(</span><span class="n">log_egopose_dict_fpath</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_egopose_dict</span><span class="p">)</span>
            <span class="n">save_pkl_dictionary</span><span class="p">(</span><span class="n">log_timestamp_dict_fpath</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">log_timestamp_dict</span><span class="p">)</span></div>

<div class="viewcode-block" id="PerFrameLabelAccumulator.accumulate_per_log_data"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.frame_label_accumulator.PerFrameLabelAccumulator.accumulate_per_log_data">[docs]</a>    <span class="k">def</span> <span class="nf">accumulate_per_log_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">log_id</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kc">None</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot;Loop through all of the logs that we have. Get the labels that pertain to the</span>
<span class="sd">        benchmark (i.e. tracking or detection) that we are interested in.</span>

<span class="sd">        We use a unique color to describe each trajectory, and then we store the</span>
<span class="sd">        instance of the trajectory, along with its color, *PER FRAME* , per log.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">MIAMI_CUBOID_COUNT</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">PITT_CUBOID_COUNT</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="n">log_fpaths</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;</span><span class="si">{self.dataset_dir}</span><span class="s2">/*&quot;</span><span class="p">)</span>
        <span class="n">log_fpaths</span> <span class="o">=</span> <span class="p">[</span><span class="n">f</span> <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">log_fpaths</span> <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">f</span><span class="p">)]</span>
        <span class="n">num_benchmark_logs</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">log_fpaths</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">log_idx</span><span class="p">,</span> <span class="n">log_fpath</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">log_fpaths</span><span class="p">):</span>
            <span class="n">log_id_</span> <span class="o">=</span> <span class="n">log_fpath</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">log_id</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">log_id_</span> <span class="o">!=</span> <span class="n">log_id</span><span class="p">:</span>
                    <span class="k">continue</span>
            <span class="k">if</span> <span class="n">log_id_</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">sdb</span><span class="o">.</span><span class="n">get_valid_logs</span><span class="p">():</span>
                <span class="k">continue</span>

            <span class="n">city_info_fpath</span> <span class="o">=</span> <span class="n">f</span><span class="s2">&quot;</span><span class="si">{self.dataset_dir}</span><span class="s2">/</span><span class="si">{log_id_}</span><span class="s2">/city_info.json&quot;</span>
            <span class="n">city_info</span> <span class="o">=</span> <span class="n">read_json_file</span><span class="p">(</span><span class="n">city_info_fpath</span><span class="p">)</span>
            <span class="n">log_city_name</span> <span class="o">=</span> <span class="n">city_info</span><span class="p">[</span><span class="s2">&quot;city_name&quot;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">log_city_name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">per_city_traj_dict</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;</span><span class="si">{log_city_name}</span><span class="s2"> not listed city&quot;</span><span class="p">)</span>
                <span class="k">continue</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">log_egopose_dict</span><span class="p">[</span><span class="n">log_id_</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">log_timestamp_dict</span><span class="p">[</span><span class="n">log_id_</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>

            <span class="n">traj_labels</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_log_trajectory_labels</span><span class="p">(</span><span class="n">log_id_</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">traj_labels</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>  <span class="c1"># skip this log since no tracking data</span>

            <span class="k">for</span> <span class="n">traj_idx</span><span class="p">,</span> <span class="n">traj_label</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">traj_labels</span><span class="p">):</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">traj_idx</span> <span class="o">%</span> <span class="mi">500</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;On traj index </span><span class="si">{traj_idx}</span><span class="s2">&quot;</span><span class="p">)</span>
                <span class="n">traj_city_fr</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">place_trajectory_in_city_frame</span><span class="p">(</span><span class="n">traj_label</span><span class="p">,</span> <span class="n">log_id_</span><span class="p">)</span>
                <span class="c1"># we don&#39;t know the city name until here</span>
                <span class="k">if</span> <span class="n">traj_idx</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;Log </span><span class="si">{log_id_}</span><span class="s2"> has {len(traj_labels)} trajectories in </span><span class="si">{log_city_name}</span><span class="s2">&quot;</span><span class="p">)</span>

                <span class="bp">self</span><span class="o">.</span><span class="n">per_city_traj_dict</span><span class="p">[</span><span class="n">log_city_name</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">traj_city_fr</span><span class="p">,</span> <span class="n">log_id_</span><span class="p">))</span>

        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;We looked at </span><span class="si">{num_benchmark_logs}</span><span class="s2"> tracking logs&quot;</span><span class="p">)</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;Miami has </span><span class="si">{MIAMI_CUBOID_COUNT}</span><span class="s2"> and Pittsburgh has </span><span class="si">{PITT_CUBOID_COUNT}</span><span class="s2"> cuboids&quot;</span><span class="p">)</span></div>

<div class="viewcode-block" id="PerFrameLabelAccumulator.get_log_trajectory_labels"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.frame_label_accumulator.PerFrameLabelAccumulator.get_log_trajectory_labels">[docs]</a>    <span class="k">def</span> <span class="nf">get_log_trajectory_labels</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">log_id</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Optional</span><span class="p">[</span><span class="n">List</span><span class="p">[</span><span class="n">TrajectoryLabel</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot; Create a very large list with all of the trajectory data.</span>

<span class="sd">        Treat a single object cuboid label as one step in a trajectory.</span>
<span class="sd">        Then we can share the same representation for both.</span>

<span class="sd">        Args:</span>
<span class="sd">            log_id (str): Log id to load.</span>

<span class="sd">        Returns:</span>
<span class="sd">            List[TrajectoryLabel]: List of trajectory labels.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">f</span><span class="s2">&quot;</span><span class="si">{self.labels_dir}</span><span class="s2">/</span><span class="si">{log_id}</span><span class="s2">/track_labels_amodal&quot;</span>
        <span class="k">if</span> <span class="n">Path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span><span class="o">.</span><span class="n">exists</span><span class="p">():</span>
            <span class="k">return</span> <span class="n">load_json_track_labels</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;</span><span class="si">{path}</span><span class="s2">/*.json&quot;</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="PerFrameLabelAccumulator.place_trajectory_in_city_frame"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.frame_label_accumulator.PerFrameLabelAccumulator.place_trajectory_in_city_frame">[docs]</a>    <span class="k">def</span> <span class="nf">place_trajectory_in_city_frame</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">traj_label</span><span class="p">:</span> <span class="n">TrajectoryLabel</span><span class="p">,</span> <span class="n">log_id</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">:</span>
        <span class="sd">&quot;&quot;&quot; Place trajectory in the city frame</span>
<span class="sd">        Args:</span>
<span class="sd">            traj_label (TrajectoryLabel): instance of the TrajectoryLabel class.</span>
<span class="sd">            log_id (str): Log id.</span>

<span class="sd">        Returns:</span>
<span class="sd">            -   traj_city_fr: trajectory length of NUM_CUBOID_VERTS (x,y,z) coords per cuboid.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">seq_len</span> <span class="o">=</span> <span class="n">traj_label</span><span class="o">.</span><span class="n">timestamps</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bboxes_3d</span><span class="p">:</span>
            <span class="n">NUM_CUBOID_VERTS</span> <span class="o">=</span> <span class="mi">8</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">NUM_CUBOID_VERTS</span> <span class="o">=</span> <span class="mi">4</span>

        <span class="c1"># store NUM_CUBOID_VERTS (x,y,z) coords per cuboid</span>
        <span class="n">traj_city_fr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">seq_len</span><span class="p">,</span> <span class="n">NUM_CUBOID_VERTS</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
        <span class="n">rand_color</span> <span class="o">=</span> <span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()),</span> <span class="nb">float</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()),</span> <span class="nb">float</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()))</span>
        <span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;On log </span><span class="si">{log_id}</span><span class="s2"> with </span><span class="si">{traj_label.track_uuid}</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">seq_len</span><span class="p">):</span>

            <span class="n">obj_label_rec</span> <span class="o">=</span> <span class="n">ObjectLabelRecord</span><span class="p">(</span>
                <span class="n">quaternion</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">quaternions</span><span class="p">[</span><span class="n">t</span><span class="p">],</span>
                <span class="n">translation</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">translations</span><span class="p">[</span><span class="n">t</span><span class="p">],</span>
                <span class="n">length</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">max_length</span><span class="p">,</span>
                <span class="n">width</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">max_width</span><span class="p">,</span>
                <span class="n">height</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">max_height</span><span class="p">,</span>
                <span class="n">occlusion</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">occlusion</span><span class="p">[</span><span class="n">t</span><span class="p">],</span>
            <span class="p">)</span>

            <span class="n">timestamp</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">traj_label</span><span class="o">.</span><span class="n">timestamps</span><span class="p">[</span><span class="n">t</span><span class="p">])</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bboxes_3d</span><span class="p">:</span>
                <span class="n">bbox_ego_frame</span> <span class="o">=</span> <span class="n">obj_label_rec</span><span class="o">.</span><span class="n">as_3d_bbox</span><span class="p">()</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">bbox_ego_frame</span> <span class="o">=</span> <span class="n">obj_label_rec</span><span class="o">.</span><span class="n">as_2d_bbox</span><span class="p">()</span>

            <span class="n">bbox_city_fr</span><span class="p">,</span> <span class="n">pose_city_to_ego</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">convert_bbox_to_city_frame</span><span class="p">(</span>
                <span class="n">timestamp</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dataset_dir</span><span class="p">,</span> <span class="n">log_id</span><span class="p">,</span> <span class="n">bbox_ego_frame</span>
            <span class="p">)</span>
            <span class="k">if</span> <span class="n">bbox_city_fr</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;</span><span class="se">\t</span><span class="s2"> </span><span class="si">{log_id}</span><span class="s2">: Couldnt find the pose for </span><span class="si">{traj_label.track_uuid}</span><span class="s2">!&quot;</span><span class="p">)</span>
                <span class="k">continue</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">log_egopose_dict</span><span class="p">[</span><span class="n">log_id</span><span class="p">][</span><span class="n">timestamp</span><span class="p">]</span> <span class="o">=</span> <span class="n">pose_city_to_ego</span>

            <span class="n">frame_rec</span> <span class="o">=</span> <span class="n">FrameRecord</span><span class="p">(</span>
                <span class="n">bbox_city_fr</span><span class="o">=</span><span class="n">bbox_city_fr</span><span class="p">,</span>
                <span class="n">bbox_ego_frame</span><span class="o">=</span><span class="n">bbox_ego_frame</span><span class="p">,</span>
                <span class="n">occlusion_val</span><span class="o">=</span><span class="n">obj_label_rec</span><span class="o">.</span><span class="n">occlusion</span><span class="p">,</span>
                <span class="n">color</span><span class="o">=</span><span class="n">rand_color</span><span class="p">,</span>
                <span class="n">track_uuid</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">track_uuid</span><span class="p">,</span>
                <span class="n">obj_class_str</span><span class="o">=</span><span class="n">traj_label</span><span class="o">.</span><span class="n">obj_class_str</span><span class="p">,</span>
            <span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">log_timestamp_dict</span><span class="p">[</span><span class="n">log_id</span><span class="p">]</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">timestamp</span><span class="p">,</span> <span class="p">[])</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">frame_rec</span><span class="p">)</span>

            <span class="n">traj_city_fr</span><span class="p">[</span><span class="n">t</span><span class="p">]</span> <span class="o">=</span> <span class="n">bbox_city_fr</span>

        <span class="k">return</span> <span class="n">traj_city_fr</span></div>

<div class="viewcode-block" id="PerFrameLabelAccumulator.convert_bbox_to_city_frame"><a class="viewcode-back" href="../../../argoverse.data_loading.html#argoverse.data_loading.frame_label_accumulator.PerFrameLabelAccumulator.convert_bbox_to_city_frame">[docs]</a>    <span class="k">def</span> <span class="nf">convert_bbox_to_city_frame</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">lidar_timestamp_ns</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">dataset_dir</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">log_id</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">bbox_ego_frame</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span>
    <span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">]]:</span>
        <span class="sd">&quot;&quot;&quot; Convert bounding box to city frame.</span>
<span class="sd">        Args:</span>
<span class="sd">            lidar_timestamp_ns (int): Lidar timestamp.</span>
<span class="sd">            dataset_dir (str): representing full path to the log_ids.</span>
<span class="sd">            log_id (str): e.g. &#39;3ced8dba-62d0-3930-8f60-ebeea2feabb8&#39;.</span>
<span class="sd">            bbox_ego_frame (np.ndarray): Numpy array of shape (4,3), representing bounding box in egovehicle frame</span>

<span class="sd">        Returned:</span>
<span class="sd">            bbox_city_fr: Numpy array of shape (4,3), representing bounding box in CITY frame</span>
<span class="sd">            pose_city_to_ego: dictionary, has two fields: &#39;translation&#39; and &#39;rotation&#39;</span>
<span class="sd">                        describing the SE(3) for p_city = city_to_egovehicle_se3 * p_egovehicle</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">city_to_egovehicle_se3</span> <span class="o">=</span> <span class="n">get_city_SE3_egovehicle_at_sensor_t</span><span class="p">(</span><span class="n">lidar_timestamp_ns</span><span class="p">,</span> <span class="n">dataset_dir</span><span class="p">,</span> <span class="n">log_id</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">city_to_egovehicle_se3</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
                <span class="n">f</span><span class="s2">&quot;Could not get city to egovehicle coordinate transformation at timestamp </span><span class="si">{lidar_timestamp_ns}</span><span class="s2">&quot;</span>
            <span class="p">)</span>

        <span class="n">bbox_city_fr</span> <span class="o">=</span> <span class="n">city_to_egovehicle_se3</span><span class="o">.</span><span class="n">transform_point_cloud</span><span class="p">(</span><span class="n">bbox_ego_frame</span><span class="p">)</span>
        <span class="n">pose_city_to_ego</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;rotation&quot;</span><span class="p">:</span> <span class="n">city_to_egovehicle_se3</span><span class="o">.</span><span class="n">rotation</span><span class="p">,</span>
            <span class="s2">&quot;translation&quot;</span><span class="p">:</span> <span class="n">city_to_egovehicle_se3</span><span class="o">.</span><span class="n">translation</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">bbox_city_fr</span><span class="p">,</span> <span class="n">pose_city_to_ego</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Argo AI, LLC

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>